package cat.music.ut.base

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import cat.music.ut.CatApp
import cat.music.ut.comm.GlideUtils

abstract class BaseRvAdapter<E : Any, V : ViewBinding>(
    val context: Context,
    val data: MutableList<E>
) :
    RecyclerView.Adapter<CommonRvHolder<V>>() {

    val sp by lazy { CatApp.getInstance().getSp() }
    val mData by lazy { data }

    /**
     * 数据源
     */
//    open var data: MutableList<E> = mutableListOf()
//        set(value) {
//            field = value
//            notifyItemRangeChanged(0, value.size)
//        }
    // 为什么不把这个方法也封装起来？ 因为不想使用反射~
    abstract override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonRvHolder<V>

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: CommonRvHolder<V>, position: Int) {
        onBindViewHolder(
            holder,
            position,
            holder.binding,
            data[position]
        )
    }

    abstract fun onBindViewHolder(holder: CommonRvHolder<V>, position: Int, vb: V, bean: E)
    fun imageRoundLoad(url: String?, view: ImageView, round: Int) {
        if (url != null) {
            GlideUtils.displayRoundImage(context, url, view, round)
        }
    }

    fun imageRoundLoad(res: Int?, view: ImageView, round: Int) {
        if (res != null) {
            GlideUtils.displayRoundImage(context, res, view, round)
        }
    }

    fun getList(): MutableList<E> {
        return data
    }

}

open class CommonRvHolder<V : ViewBinding>(val binding: V) : RecyclerView.ViewHolder(binding.root) {

}